<?php
require_once ROOT_LIBRARY_UTILITY.DS. 'BaseController.php';
require_once ROOT_LIBRARY_UTILITY.DS. 'Globals.php';

require_once ROOT_APPLICATION_MODULE_BACKEND_MODELS.DS. 'Model_Account.php';
require_once ROOT_APPLICATION_MODULE_BACKEND_MODELS.DS. 'Model_Article.php';
require_once ROOT_APPLICATION_MODULE_BACKEND_MODELS.DS. 'Model_Article_Type.php';

class ArticleController extends BaseController{

	public function init(){
		parent::init();

	}

	public function indexAction(){
        try
        {
            if($this->_request->getMethod() == "POST")
            {

            }
            else
            {
                $mdArticle = new Model_Article();
                $mdArticleType = new Model_Article_Type();
                $articleType = $mdArticleType->_getAll(" parent_id = 0 ");
                
                $listId = array();
                $data = array();
                foreach ($articleType as $key => $item)
                    $listId[] = $item->id;
                
                array_multisort($articleType, SORT_DESC, $listId);
                $articleType = array_slice($articleType, 0, 3);

                foreach ($articleType as $key => $item)
                {
                    $data[$key] = $mdArticle->getAllByArticleId($item->id);
                }
                $this->view->listArticle = $data;                
                $this->view->articleType = $articleType;
            }
        }
        catch(Exception $ex)
        {

        }
	}

    public function detailAction()
    {
        try
        {
            $articleId = $this->_request->getParam('id');

            $mdArticle = new Model_Article();
            $article = $mdArticle->_getByKey($articleId);
            $this->view->article = $article;

            $mdArticleType = new Model_Article_Type();
            $this->view->articleType = $mdArticleType->_getByKey($article->article_types_id);
            $this->view->listArticle = $mdArticle->_getAll(" article_types_id = $article->article_types_id AND id != $articleId");

            $mdAccount = new Model_Account();
            $this->view->createdBy = $mdAccount->_getByKey($article->created_by);
        }
        catch(Exception $ex)
        {

        }
    }
}